package com.lisx.module_time_block.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lisx.module_time_block.bean.NewBlockIconBean;
import com.lisx.module_time_block.databinding.ItemNewBlockIconBinding;

import java.util.List;

public class NewBlockIconAdapter extends RecyclerView.Adapter<NewBlockIconAdapter.MyViewHolder> {

    List<NewBlockIconBean> iconData;

    public NewBlockIconAdapter(List<NewBlockIconBean> iconData) {
        this.iconData = iconData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNewBlockIconBinding binding = ItemNewBlockIconBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.binding.ivRes.setImageResource(iconData.get(position).res);
        holder.binding.container.setSelected(iconData.get(position).isSelect);
        holder.binding.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (NewBlockIconBean newBlockIconBean : iconData) {
                    newBlockIconBean.isSelect = false;
                }
                iconData.get(position).isSelect = true;
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return iconData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemNewBlockIconBinding binding;

        public MyViewHolder(@NonNull ItemNewBlockIconBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
